home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_2.3 / COMBINE / COMBINE.C next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  4.0 KB  |  170 lines

  1. /* 
  2.  * combine.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * COMBINE (Image addition)
  11.  *
  12.  *  Adds two input images and places the resulting
  13.  *  image in the output file
  14.  *  Image addition is clipped at 255 by default
  15.  */
  16.  
  17. #include "combine.h"
  18.  
  19. #define  NOLOOP        0
  20. #define INDEX_SCALE 255
  21.  
  22. #define     ON        1
  23. #define     OFF        0
  24.  
  25. /*
  26.  * global variables
  27.  */
  28. extern char *optarg;
  29. extern int optind, opterr;
  30. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  31.  
  32. int BINARIZE = OFF;
  33.  
  34. /*
  35.  * usage of routine
  36.  */
  37. void
  38. usage (char *progname)
  39. {
  40.   progname = last_bs (progname);
  41.   printf ("USAGE: %s in1img in2img outimg [-c clip][-L]\n", progname);
  42.   printf ("\n%s adds two images and writes the resulting\n", progname);
  43.   printf ("image to the specified output file.\n\n");
  44.   printf ("ARGUMENTS:\n");
  45.   printf ("   in1img: first input image filename (TIF)\n");
  46.   printf ("   in2img: second input image filename (TIF)\n");
  47.   printf ("   outimg: output image filename (TIF)\n\n");
  48.   printf ("OPTIONS:\n");
  49.   printf ("  -c clip: clipping factor for output image 0-255(default=%d)\n", INDEX_SCALE);
  50.   printf ("       -L: print Software License for this module\n");
  51.   exit (1);
  52. }
  53.  
  54. void
  55. add_image (Image * imgIn1, Image * imgIn2, Image * imgOut, unsigned char clip_factor)
  56. {
  57.   int ix, iy;
  58.   int max_x, max_y;
  59.   unsigned long pixelOut;
  60.  
  61.   max_y = ImageGetHeight (imgIn1);
  62.   max_x = ImageGetWidth (imgIn1);
  63.   for (ix = 0; ix < max_x; ix++)
  64.     for (iy = 0; iy < max_y; iy++) {
  65.       pixelOut = getpixel (ix, iy, imgIn1) + getpixel (ix, iy, imgIn2);
  66.       setpixel (ix, iy, imgOut, (pixelOut > clip_factor ? clip_factor : (unsigned char) pixelOut));
  67.     }
  68. }
  69.  
  70. void
  71. main (int argc, char *argv[])
  72. {
  73.   Image *imgIn1;
  74.   Image *imgIn2;
  75.   Image *imgOut;
  76.  
  77.   int i_arg;
  78.   int clip_factor = INDEX_SCALE;
  79.   int binarize = 0;
  80.   int outheight;
  81.   int outwidth;
  82.  
  83. /*
  84.  * cmd line options
  85.  */
  86.   static char *optstring = "c:bL";
  87.  
  88.  
  89. /*
  90.  * parse command line
  91.  */
  92.   optind = 4;                   /* set getopt to point to the 4th arg */
  93.   opterr = ON;                  /* give error messages */
  94.  
  95.  
  96.   if (argc < 4)
  97.     usage (argv[0]);
  98.  
  99.   while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
  100.     switch (i_arg) {
  101.     case 'b':
  102.       printf ("\n...option %c: generate binary output\n", i_arg);
  103.       binarize = 1;
  104.       break;
  105.     case 'c':
  106.       if ((clip_factor = atoi (optarg)) > 255) {
  107.         printf ("Clipping factor must be <= 255\n");
  108.         printf ("Defaulting to clipping factor = %d\n", INDEX_SCALE);
  109.         clip_factor = INDEX_SCALE;
  110.       }
  111.       break;
  112.     case 'L':
  113.       print_sos_lic ();
  114.       exit (0);
  115.     default:
  116.       printf ("\ngetopt: unknown condition encountered\n");
  117.       exit (1);
  118.       break;
  119.     }
  120.   }
  121.  
  122. /*
  123.  * Read input image
  124.  */
  125.   imgIn1 = ImageIn (argv[1]);
  126.  
  127.   if (imgIn1->bps == 8 && imgIn1->spp == 3) {
  128.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  129.     exit (1);
  130.   }
  131.  
  132. /*
  133.  * Read reference image
  134.  */
  135.   imgIn2 = ImageIn (argv[2]);
  136.  
  137.   if (imgIn2->bps == 8 && imgIn2->spp == 3) {
  138.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  139.     exit (1);
  140.   }
  141.  
  142.   if (ImageGetHeight (imgIn1) != ImageGetHeight (imgIn2) ||
  143.       ImageGetWidth (imgIn1) != ImageGetWidth (imgIn2)) {
  144.     printf ("Input and Reference images have different sizes\n");
  145.     printf ("Minimum size will be chosen for output image\n");
  146.   }
  147.   outheight = (ImageGetHeight (imgIn1) > ImageGetHeight (imgIn2)) ? ImageGetHeight (imgIn2) : ImageGetHeight (imgIn1);
  148.   outwidth = (ImageGetWidth (imgIn1) > ImageGetWidth (imgIn2)) ? ImageGetWidth (imgIn2) : ImageGetWidth (imgIn1);
  149.  
  150. /*
  151.  * Allocate memory for output image
  152.  */
  153.   imgOut = ImageAlloc ((long) outheight, (long) outwidth, (long) BPS);
  154.  
  155. /*
  156.  * perform background illumination correction 
  157.  */
  158.   add_image (imgIn1, imgIn2, imgOut, (unsigned char) clip_factor);
  159.  
  160. /* 
  161.  * reset tiffInput so that we write a grayscale file (i.e tags are not copied)
  162.  */
  163.   tiffInput = 0;
  164.  
  165. /*
  166.  * Write the output image
  167.  */
  168.   ImageOut (argv[3], imgOut);
  169. }
  170.